package com.atguigui.leetcode;

/**
 * 814.二叉树剪枝
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/7/21 8:48
 */
public class P814BinaryTreePruning {
    public static void main(String[] args) {
        Solution solution = new P814BinaryTreePruning().new Solution();
        // TO TEST
    }


    class Solution {
        public TreeNode pruneTree(TreeNode root) {
            if (root == null) {
                return null;
            }
            root.left = pruneTree(root.left);
            root.right = pruneTree(root.right);
            //左子树为空，右子树为空，当前节点的值为 00，同时满足时，才表示以当前节点为根的原二叉树的所有节点都为 00，需要将这棵子树移除，返回空。
            if (root.left == null && root.right == null && root.val == 0) {
                return null;
            }
            return root;
        }
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
